package com.chen.controller;

import com.chen.entity.Result;
import com.chen.exception.MyException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author zhebie
 * @DATE 2021/9/17
 */
@RestControllerAdvice
public class MyExceptionAdvice {
    private static final Logger log = LoggerFactory.getLogger(MyExceptionAdvice.class);

    /**
     *用来捕获MyException类型的异常
     */
    @ExceptionHandler(MyException.class)
    public Result handleMyException(MyException e){
        log.info("进入了自定义异常处理，用户行为不规范");
        //返回给前端提示信息
        return new Result(false,e.getMessage());
    }
    /**
     * 捕获Exception类型的异常
     */
    @ExceptionHandler(Exception.class)
    public Result handleException(Exception e){
        log.error("发生了未知异常",e);
        //给前端返回提示信息
        return new Result(false,"发生未知异常，请联系管理员");
    }
    /**
     * 捕获AccessDeniedException类型的异常
     */
    @ExceptionHandler(AccessDeniedException.class)
    public Result handleAccessDeniedException(AccessDeniedException e){
        //给前端返回提示信息
        return new Result(false,"权限不足，无法访问！");
    }
}
